package com.adv.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.adv.adapter.DistributorAdapter;
import com.adv.object.DistributorObj;

public class Distributors extends SherlockActivity {

	protected static final String DISTRIBUTOR_ID_KEY = "distributorID";
	protected static final String DISTRIBUTOR_ICON_CACHE = "distributorIconCache";
	private List<DistributorObj> objects;
	private ActionBar actionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_distributors);
		
		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setSubtitle("Nhà cung cấp");
		
		//TODO: catalog_id to query DB
		Intent retrive=getIntent();
		Log.e(getPackageName(), "Distributor <- get ID:"+retrive.getIntExtra(Catalog.CATALOG_ID_KEY, -1));
		
		// TODO: set value from Json
		objects=new ArrayList<DistributorObj>();
		objects.add(new DistributorObj(1, "pICO", "http://pico.vn/Images/loginLogo.png", 1));
		objects.add(new DistributorObj(1, "pICO", "http://pico.vn/Images/loginLogo.png", 1));
		objects.add(new DistributorObj(1, "pICO", "http://pico.vn/Images/loginLogo.png", 1));
		objects.add(new DistributorObj(1, "pICO", "http://pico.vn/Images/loginLogo.png", 1));
		
		ListView  lDistributor=(ListView)findViewById(R.id.list_distributor);
		lDistributor.setAdapter(new DistributorAdapter(this, R.layout.base_item, objects));
		lDistributor.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View v, int pos,
					long id) {
				Intent i=new Intent(Distributors.this, Details.class);
				i.putExtra(DISTRIBUTOR_ID_KEY, objects.get(pos).getId());
				i.putExtra(DISTRIBUTOR_ICON_CACHE, objects.get(pos).getIcon());
				
				Log.e(getPackageName(), "Distributor Activity: send ID : "+objects.get(pos).getId());
				Log.e(getPackageName(), "Distributor Activity: send Icon cache : "+objects.get(pos).getIcon());
				startActivity(i);
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId,
			com.actionbarsherlock.view.MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
		} else if (item.getItemId() == R.id.action_home) {
			startActivity(new Intent(Distributors.this, Catalog.class));
			finish();
		}
		return true;
	}
}
